package com.yupi.project.scheduled;

import com.google.gson.Gson;
import com.mdd.model.entity.InterfaceInfo;
import com.yupi.project.common.ResultUtils;
import com.yupi.project.mapper.UserInterfaceInfoMapper;
import com.yupi.project.model.vo.InterfaceInfoVo;
import com.yupi.project.service.InterfaceInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Mdd
 * @Github <a href="https://github.com/mdd123cc"/>
 * @Gitee <a href="https://gitee.com/mdd115192"/>
 */

@Component
public class SchedulingTask {

    private static final long FLUSH_TIME = 1000*60*60*12;

    /**
     * 接口信息key
     */
    private static final String INTERFACEINFO_LIST_KEY = "interfaceInfo:list";

    /**
     * 接口调用排行榜key
     */
    public static final String INTERFACE_INVOKE_TOP_KEY = "interfaceInfo:invoke:top";

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private InterfaceInfoService interfaceInfoService;

    @Resource
    private UserInterfaceInfoMapper userInterfaceInfoMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 每隔12小时刷新一次接口信息
     */
    // todo 部署时开启
    // @Scheduled(fixedRate = FLUSH_TIME)
    public void flushInterfaceInfo(){
        //获取接口信息列表
        List<InterfaceInfo> interfaceInfoList = interfaceInfoService.getInterfaceInfoList();
        List<InterfaceInfo> interfaceInfos = interfaceInfoList.stream().map(interfaceInfo -> {
            InterfaceInfo interfaceInfo1 = new InterfaceInfo();
            interfaceInfo1.setId(interfaceInfo.getId());
            interfaceInfo1.setHost(interfaceInfo.getHost());
            interfaceInfo1.setPath(interfaceInfo.getPath());
            interfaceInfo1.setUrl(interfaceInfo.getUrl());
            return interfaceInfo1;
        }).collect(Collectors.toList());
        Gson gson = new Gson();
        String jsonStr = gson.toJson(interfaceInfos);
        stringRedisTemplate.opsForValue().set(INTERFACEINFO_LIST_KEY,jsonStr);
    }
    /**
     * 每隔6小时刷新一次全站接口调用情况
     */
    // todo 部署时开启
    // @Scheduled(fixedRate = FLUSH_TIME)
    public void flushInterfaceTopByInvoke(){
        ZSetOperations<String, String> zSetOperations = redisTemplate.opsForZSet();
        //获取前limit个调用最多的接口
        List<InterfaceInfoVo> interfaceInfoVos = userInterfaceInfoMapper.getListTopInvokeInterfaceInfo(4);
        interfaceInfoVos.forEach(interfaceInfoVo -> {
            long id = interfaceInfoVo.getId();
            Integer totalNum = interfaceInfoVo.getTotalNum();
            zSetOperations.add(INTERFACE_INVOKE_TOP_KEY,id+"",totalNum);
        });
    }
}
